package com.ty.consumer;

import cn.hutool.json.JSONUtil;
import com.ty.dto.OrderOk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

//@Configuration
//@Slf4j
public class DeadConsumer {

    //@Autowired
    private AmqpAdmin ramqAdmin;

    @PostConstruct
    public void init(){
        boolean unused = false;
        boolean empty = false;
       ramqAdmin.deleteQueue("Q1",unused,empty);
    }

    //定义一个正常业务队列 持久化 Q1
    @Bean
    public Queue orderingQ1(){
        return QueueBuilder.durable("ordering-Q1")
                .deadLetterExchange("dead-ex")
                .deadLetterRoutingKey("dead_rk")
               // .ttl(10*1000)  //ttl 是队列的属性 ，所有的消息在队列里停留的时长为10s,10s过后就成为死信  --针对所有消息
               // .maxLength(10)  //定义队列的最大容量，超过这个容量，就会把前面的消息变成死信，投递给死信交换机
                .build();
    }

    //定义一个死信队列
    @Bean
    public Queue deadQ1(){
        return QueueBuilder.durable("dead-Q1").build();
    }
    //定义一个交换机Direct-ex  持久化 Q1
    @Bean
    public Exchange orderingExchange(){
        return ExchangeBuilder.directExchange("ordering-ex").durable( true).build();
    }
    //定义一个死信交换机
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.directExchange("dead-ex").durable( true).build();
    }

    //队列和交换机的绑定关系
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(orderingQ1()).to(orderingExchange()).with("rk01").noargs();
    }
    @Bean
    public Binding binding_dead(){
        return BindingBuilder.bind(deadQ1()).to(deadExchange()).with("dead_rk").noargs();
    }


    //@RabbitListener(queues = "ordering-Q1")
    public void consumer1(OrderOk msg){
        System.out.println("ordering1接收到消息："+msg);
        //log.debug("消费者->{}", JSONUtil.toJsonStr(msg));
        int i = 1/0;
    }



}
